//
//  DetailView.swift
//  MovieAppDemo
//
//  Created by Demo on 2025/1/26.
//

import SwiftUI

struct ComicView: View {
    var comicpicstest: [ComicPics] = comicpics
    @State var item: MainPica = mainpicas[0]
    @Environment(\.dismiss) var dismiss
    @StateObject private var viewModel = ComicPicsViewModel()
    @State private var hasAppeared = false
    @State private var showControlBar = false
    @State private var showProgressBar = false
    @State private var showScreenProgress = true
    @State var ep_id: Int = 238729
    @State var ep_name: String = "章节名称"
    // 用于显示当前图片和总图片数
    @State private var currentIndex: Int = 0
    @State private var currentAll: Int = 0
    @State var selectIndex: Int = 1
    @State private var auto_read = false
    @State private var timer: Timer? = nil
    @State var is_fav: Int = 0
    
    
    var body: some View {
        
        ZStack(alignment: .top) {
            VStack {
                ScrollView(showsIndicators: false) {
                    ComicViewDetail(item:$item,auto_read:$auto_read)
                    ComicViewPics(viewModel: viewModel, currentIndex: $currentIndex, ep_id: $ep_id, hasAppeared: $hasAppeared,currentAll:$currentAll,selectIndex: $selectIndex)
                }
            }
            .navigationBarBackButtonHidden(true)
            .ignoresSafeArea()
            
            ComicViewControlBar(item:$item,ep_name: $ep_name,showControlBar: $showControlBar,currentIndex:$currentIndex,currentAll:$currentAll,selectIndex: $selectIndex, ep_id: $ep_id,is_fav: $is_fav)
            ComicViewProgress(showScreenProgress:$showScreenProgress,currentIndex:$currentIndex,currentAll:$currentAll)
        }
        .onTapGesture {
            withAnimation(.easeInOut(duration: 0.2)) {  // 设置持续时间为0.2秒，动画会更快
                showControlBar.toggle()
                auto_read = false
            }
        }
        .onChange(of: showControlBar) { oldValue, newValue in
            if newValue {
                showScreenProgress = false
            } else {
                showScreenProgress = true
            }
        }
        
        .onChange(of: auto_read) { oldValue, newValue in
            if newValue {
                // 启动定时器，每秒更新 currentIndex
                selectIndex = 0
                
                timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { _ in
                    if selectIndex < currentAll {
                        withAnimation(.easeInOut(duration: 0.5)) {
                            selectIndex += 1
                            print(selectIndex)
                        }
                    } else {
                        // 如果 currentIndex 达到 currentAll，停止定时器
                        timer?.invalidate()
                        timer = nil
                        auto_read = false // 关闭自动阅读
                    }
                }
               
            } else {
                // 停止定时器
                timer?.invalidate()
                timer = nil
            }
        }
        .onDisappear {
            timer?.invalidate()
        }

        
    }
}



#Preview {
    NavigationView {
        ComicView()
    }
}
